package com.cat.dataStructure;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/sum-of-absolute-differences-in-a-sorted-array/
 * @create 2025/8/19 15:09
 * @since JDK17
 */

public class Solution36 {
    public int[] getSumAbsoluteDifferences(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n], prefix = new int[n + 1];
        for (int i = 0; i < n; i++) {
            prefix[i + 1] = prefix[i] + nums[i];
        }
        for (int i = 0; i < n; i++) {
            ans[i] += i * nums[i] - prefix[i];
            ans[i] += prefix[n] - prefix[i + 1] - (n - i - 1) * nums[i];    // 后半部分
        }
        return ans;
    }
}
